package modulesAPI.v2.basics;
import java.awt.Dimension;

import com.caterina.components.CPanel;
import com.caterina.resources.CBorders;
import com.caterina.resources.CColors;
import com.caterina.resources.CModules;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;

public class ModTextArea extends Module<String> implements DocumentListener{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di stringhe lunghe
	 * */
	public ModTextArea(Module<?> ownerModule, String content, String tag,String description, ViewType viewType) {
		super(ownerModule, content, tag, description, viewType);
		this.createTxtArea();
	}
	private void createTxtArea(){
		this.txtArea=new JTextPane();
		this.txtArea.setText(this.getContent());
		txtArea.getDocument().addDocumentListener(this);
		this.txtArea.setSelectedTextColor(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		this.txtArea.setSelectionColor(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
		txtArea.setMaximumSize(new Dimension(300,5000));
		txtArea.setBorder(CBorders.FIELDS_BORDER);
		txtArea.setMinimumSize(new Dimension(300,1));
		txtArea.setPreferredSize(new Dimension(300,100));
		txtArea.getDocument().addDocumentListener(this);
		this.scrollPane=new JScrollPane(txtArea);
		scrollPane.setMaximumSize(new Dimension(300,100));
		scrollPane.setMinimumSize(new Dimension(300,1));
		scrollPane.setPreferredSize(new Dimension(300,100));
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return CModules.formatPanelForReadOnlyPairs(this.getTag(), this.getContent());
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return  CModules.formatForPairs(this.getTag(), scrollPane);
	}
	@Override
	public void changedUpdate(DocumentEvent e) {}
	@Override
	public void insertUpdate(DocumentEvent e) {
		this.setContent(txtArea.getText());
		this.fireModuleChanged(this);
	}
	@Override
	public void removeUpdate(DocumentEvent e) {
		this.setContent(txtArea.getText());
		this.fireModuleChanged(this);
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return null;
	}
	@Override
	protected void afterFireChange(Module<?> mod) {}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return this.getReadOnlyPanel(isSelected);
	}
	private JScrollPane scrollPane;
	private JTextPane txtArea;
}